<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class:{'jee-hidden': disableSubmit} }"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row>
              <a-col :span="24">
                <a-form-model-item label="品牌名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brandName">
                  <a-input v-model="model.brandName" placeholder="请输入品牌名称"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="品牌图标" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brandImage">
                  <j-image-upload  v-model="model.brandImage" ></j-image-upload>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="是否首页推荐" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isRecommand">
                  <j-switch v-model="model.isRecommand"  ></j-switch>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="brandSort">
                  <a-input-number v-model="model.brandSort" placeholder="请输入排序" style="width: 100%" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  export default {
    name: 'YlMachineryBrandModal',
    components: {},
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        title:'',
        width:800,
        visible: false,
        disableSubmit: false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           brandName: [
              { required: true, message: '请输入品牌名称!'},
           ],
           brandImage: [
              { required: true, message: '请输入品牌图标!'},
           ],
           isRecommand: [
              { required: true, message: '请输入是否首页推荐!'},
           ],
           brandSort: [
              { required: true, message: '请输入排序!'},
           ],
        },
        url: {
          add: "/machinery/ylMachineryBrand/add",
          edit: "/machinery/ylMachineryBrand/edit",
          queryById: "/machinery/ylMachineryBrand/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.visible=true
        this.$nextTick(()=>{
          this.edit(this.modelDefault);
        })
      },
      edit (record) {
        this.visible=true
        this.$nextTick(()=>{
          this.model = Object.assign({}, record);
          this.visible = true;
        })
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        this.submitForm();
      },
      submitForm () {
          const that = this;
          // 触发表单验证
          this.$refs.form.validate(valid => {
            if (valid) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
              }else{
                httpurl+=this.url.edit;
                 method = 'put';
              }
              httpAction(httpurl,this.model,method).then((res)=>{
                if(res.success){
                  that.$message.success(res.message);
                  that.submitCallback();
                }else{
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.confirmLoading = false;
              })
            }

          })
      },
      submitCallback(){
        this.$emit('ok');
        this.visible = false;
      },
      handleCancel () {
        this.close()
      }
    }
  }
</script>
